package com.example.onlineeducation.utils

import android.text.TextUtils
import com.yc.jetpacklib.extension.ycIsEmpty
import com.yc.jetpacklib.utils.YcTime
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-04-07
 * UseDes:
 */
object YcTimeUtils {


    /*
     * Long毫秒数 ->Str
     * millisecond 总时间(毫秒) ->  00:00:00或者00:00
     * 15020->00:00:00或者00:00
     */

    fun intMillisecondToStr(millisecond: Int): String {
        val second = (millisecond / 1000).toLong() //将毫秒转换为秒
        val hh = second / 3600 //计算小时
        val mm = second % 3600 / 60 //计算分钟
        val ss = second % 60 //计算秒
        //判断时间单位的位数
        val str: String = if (hh != 0L) { //表示时间单位为三位
            String.format("%02d:%02d:%02d", hh, mm, ss)
        } else {
            String.format("%02d:%02d", mm, ss)
        }
        return str
    }


    fun intSecondToMinStr(second: Int): String {
        val hh = second / 3600 //计算小时
        val mm = second % 3600 / 60 //计算分钟
        val ss = second % 60 //计算秒
        //判断时间单位的位数
        val str: String = if (hh != 0) { //表示时间单位为三位
            String.format("%02d小时%02d分钟", hh, mm)
        } else {
            String.format("%02d分钟", mm)
        }
        return str
    }

    fun intSecondToSecondStr(second: Int): String {
        val hh = second / 3600 //计算小时
        val mm = second % 3600 / 60 //计算分钟
        val ss = second % 60 //计算秒
        //判断时间单位的位数
        val str: String = if (hh != 0) { //表示时间单位为三位
            String.format("%02d小时%02d分钟%02d秒", hh, mm, ss)
        } else {
            String.format("%02d分钟%02d秒", mm, ss)
        }
        return str
    }

    /*
    * 时间比大小
    * false：time1超过time2
    * */
    @JvmStatic
    fun compareSmaller(time1: String?, time2: String?, formatTime1: String = YcTime.FORMAT_TIME_SECOND, formatTime2: String = YcTime.FORMAT_TIME_SECOND): Boolean {
        return try {
            if (time1.ycIsEmpty() || time2.ycIsEmpty()) {
                false
            } else {
                val newTime1 = SimpleDateFormat(formatTime1).parse(time1)
                val newTime2 = SimpleDateFormat(formatTime2).parse(time2)
                newTime1.time < newTime2.time
            }
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }

    /**
     * 时间格式转换
     * @param time String?          输入的时间
     * @param defaultOut String     默认值
     * @param formatInput String    输入的格式
     * @param formatOut String      输出的格式
     */
    @JvmStatic
    fun stringToString(
        time: String?,
        defaultOut: String = "",
        formatInput: String = YcTime.FORMAT_TIME,
        formatOut: String = YcTime.FORMAT_TIME_MONTH_DAY
    ): String {
        if (time == null) {
            return defaultOut
        }
        val sdf = SimpleDateFormat(formatInput, Locale.getDefault())
        return try {
            val timeDate = sdf.parse(time)
            SimpleDateFormat(formatOut, Locale.getDefault()).format(timeDate)
        } catch (e: ParseException) {
            e.printStackTrace()
            defaultOut
        }
    }

    @JvmStatic
    fun dateToDate(date: Date, format: String = YcTime.FORMAT_TIME): Date? {
        return YcTime.stringToDate(YcTime.dateToString(date,format))
    }

    //获取当前时间前几天
    fun getDateInterval(interval: Int, formatTime: String = YcTime.FORMAT_TIME): String {
        val format = SimpleDateFormat(formatTime, Locale.getDefault())
        val c = Calendar.getInstance()
        c.time = Date()
        c.add(Calendar.DATE, -interval)
        val start = c.time
        return format.format(start)
    }

    @JvmStatic
    fun secondToString(second: Long): String {
        return if (second == 0L) {
            "-"
        } else {
            val days = second / (3600 * 24)
            val hh = second / 3600
            val mm = second % 3600 / 60
            val ss = second % 60
            if (days > 0) {
                if (hh > 0) {
                    String.format("%d天%d时", days, hh)
                } else {
                    String.format("%d天", days)
                }
            } else {
                if (hh > 0) {
                    if (mm > 0) {
                        String.format("%d时%d分", hh, mm)
                    } else {
                        String.format("%d时", hh)
                    }
                } else {
                    if (mm > 0) {
                        if (ss > 0) {
                            String.format("%d分%d秒", mm, ss)
                        } else {
                            String.format("%d分", mm)
                        }
                    } else {
                        String.format("%d秒", ss)
                    }
                }
            }
        }
    }
}